package com.spark.trouble.message

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.View
import com.google.gson.Gson
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.helper.MutableStatePagerAdapter
import com.spark.common.model.MessageListModel
import com.spark.trouble.*
import com.spark.trouble.handle.LimitHandleFragment
import kotlinx.android.synthetic.main.act_handle.*

/**
 * 消息-隐患
 * [任务类型]
 * 1 -> 标准化检查 -> 主任务
 * 2 -> 标准化检查 -> 子任务
 * 3 -> 借阅申请
 * 4 -> 文件传输
 * 5 -> 归档申请
 * 6 -> 隐患 -> 主任务
 * 7 -> 隐患 -> 子任务(未分配)
 * 8 ->     -> 现场治理
 * 9 ->     -> 限时整改
 * 10 ->    -> 挂牌督办
 * 11 ->    -> 隐患升级
 * 12 ->    -> 重大隐患
 * @info 处理流程状态
 *  1 -> 待处理
 *  2 -> 审核中/待审核
 *  3 -> 被驳回
 *  4 -> 确认中/待确认
 *  5 -> 整改中/待整改
 *  6 -> 验收中/待验收
 *  7 -> 验收驳回
 *  8 -> 待存储
 *  9 -> 已存储
 *  10 -> 已归档
 */
class HTMessageFragment : BaseFragment() {
    private lateinit var messageAdapter: MutableStatePagerAdapter

    companion object {
        fun newInstance(data: String): HTMessageFragment {
            val target = HTMessageFragment()
            val args = Bundle()
            args.putString("data", data)
            target.arguments = args
            return target
        }
    }

    override fun contentResId(): Int {
        return R.layout.act_handle
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        iv_back.setOnClickListener { activity?.finish() }
        arguments?.apply {
            getString("data")?.apply {
                val prop = this
                Gson().fromJson(this, MessageListModel.ResultMap::class.java)?.apply {
                    val tabTitle = mutableListOf<String>()
                    val fragments = mutableListOf<Fragment>()
                    // 整改超时
                    // 挂牌督办
                    tabTitle.apply {
                        add("通知内容")
                        add("整改通知书")
                    }
                    fragments.apply {
                        add(RehandleNotifyFragment.newInstance())
                        add(LimitHandleFragment.newInstance(Const.htTypeProcess, prop,"8"))
                    }
                    // 重大隐患治理
                    tabTitle.apply {
                        add("方案简述")
                        add("现场情况")
                        add("责任内容")
                        add("治理方案")
                        add("预案措施")
                        add("资金方案")
                    }
                    fragments.apply {
                        add(SchemaBriefFragment.newInstance("$id"))
                        add(HTInfoFragment.newInstance(Const.htTypeProcess, "$id", "7"))
                        add(ResponsbleContentFragment.newInstance("$id"))
                    }



                    tabTitle.forEach {
                        tabs.addTab(tabs.newTab().setText(it))
                    }
                    messageAdapter = object : MutableStatePagerAdapter(childFragmentManager) {
                        override fun getCount(): Int {
                            return tabTitle.size
                        }

                        override fun getPageTitle(position: Int): CharSequence? {
                            return tabTitle[position]
                        }
                    }
                    messageAdapter.adds(fragments)
                    pager.apply {
                        adapter = messageAdapter
                        canScroll = false
                    }
                    tabs.setupWithViewPager(pager)
                }
            }
        }


    }

}